package com.atguigu.web;

import com.atguigu.pojo.Food;
import com.atguigu.service.FoodService;
import com.atguigu.service.impl.FoodServiceImpl;
import com.atguigu.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ClientFoodServlet extends BaseServlet{

    private FoodService foodService = new FoodServiceImpl();

    /**
     * 处理分页的功能
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void listLimit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        // 1、调用Service层中的查询分页的菜品信息   通过传入的id判断是第几页
        List<Food> foods = foodService.queryLimitFoods(WebUtils.parseInt(id), 4);
        long count = foodService.getCount();
        // 2、将得到的信息写入request域中
        req.setAttribute("msg", foods);
        if (count % 4 == 0) {
            req.setAttribute("pages", count / 4);
        } else {
            req.setAttribute("pages", count / 4 + 1);
        }
        req.setAttribute("number", count);
        // 3、请求转发到  /pages/client/index.jsp
        req.getRequestDispatcher("/pages/client/index.jsp").forward(req, resp);
    }

    /**
     * 价格搜索
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void priceSearch(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        // 1、获得最大值和最小值的参数
        String max = req.getParameter("max");
        String min = req.getParameter("min");
        String id = req.getParameter("id");
        // 2、得到相应的food对象
        List<Food> foods = foodService.queryPriceFoods(WebUtils.parseInt(min), WebUtils.parseInt(max), WebUtils.parseInt(id),4);
        // 得到价格区间内的菜品数量
        int count = foodService.queryPriceCount(WebUtils.parseInt(min), WebUtils.parseInt(max)).size();
        // 3、将搜索到的Food写入到request域中
        req.setAttribute("msg", foods);
        if (count % 4 == 0) {
            // 用于显示数据共几页
            req.setAttribute("pages", count / 4);
        } else {
            req.setAttribute("pages", count / 4 + 1);
        }
        // 用于显示数据共几条
        req.setAttribute("number", count);

        // 4、请求转发到 /pages/client/index.jsp
        req.getRequestDispatcher("/pages/client/index.jsp").forward(req, resp);
    }

    /**
     * 处理分类后的分页功能
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void listLimitClass(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        String food_id = req.getParameter("food_id");
        // 1、调用Service层中的查询分页的菜品信息   通过传入的id判断是第几页
        List<Food> foods = foodService.queryLimitClassFoods(Integer.valueOf(food_id), WebUtils.parseInt(id), 4);
        long count = foodService.getClassCount(Integer.valueOf(food_id));
        // 2、将得到的信息写入request域中
        req.setAttribute("msg", foods);
        if (count % 4 == 0) {
            req.setAttribute("pages", count / 4);
        } else {
            req.setAttribute("pages", count / 4 + 1);
        }
        req.setAttribute("number", count);
        // 3、请求转发到  /pages/client/index.jsp
        req.getRequestDispatcher("/pages/client/index_class.jsp").forward(req, resp);
    }

    /**
     * 分类后的价格搜索
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void priceSearchClass(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        // 1、获得最大值和最小值的参数
        String max = req.getParameter("max");
        String min = req.getParameter("min");
        String id = req.getParameter("id");
        String food_id = req.getParameter("food_id");
        // 2、得到相应的food对象
        List<Food> foods = foodService.queryPriceClassFoods(Integer.valueOf(food_id), WebUtils.parseInt(min), WebUtils.parseInt(max), WebUtils.parseInt(id),4);
        System.out.println(foods);
        // 得到价格区间内的菜品数量
        int count = foodService.queryPriceClassCount(Integer.valueOf(food_id), WebUtils.parseInt(min), WebUtils.parseInt(max)).size();
        // 3、将搜索到的Food写入到request域中
        req.setAttribute("msg", foods);
        if (count % 4 == 0) {
            // 用于显示数据共几页
            req.setAttribute("pages", count / 4);
        } else {
            req.setAttribute("pages", count / 4 + 1);
        }
        // 用于显示数据共几条
        req.setAttribute("number", count);

        // 4、请求转发到 /pages/client/index.jsp
        req.getRequestDispatcher("/pages/client/index_class.jsp").forward(req, resp);
    }

}
